//package com.sping.gobang.common.config.interceptor;
//
//import com.sping.gobang.pojo.datapbject.User;
//import com.sping.gobang.service.UserService;
//import jakarta.annotation.Resource;
//import org.springframework.http.server.ServerHttpRequest;
//import org.springframework.http.server.ServerHttpResponse;
//import org.springframework.http.server.ServletServerHttpRequest;
//import org.springframework.stereotype.Component;
//import org.springframework.web.socket.WebSocketHandler;
//import org.springframework.web.socket.server.HandshakeInterceptor;
//
//import java.util.Map;
//
//@Component
//// 在握手阶段，TokenHandshakeInterceptor可以从HTTP请求中获取用户信息并存入WebSocket会话属性
//public class TokenHandshakeInterceptor implements HandshakeInterceptor {
//    @Resource(name = "userServiceImpl")
//    private UserService userService;
//    @Override
//    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
//        if(request instanceof ServletServerHttpRequest){
//            ServletServerHttpRequest servletServerHttpRequest=(ServletServerHttpRequest) request;
////          从HTTP请求属性中获取用户信息
//            String currentUsername =(String) servletServerHttpRequest.getServletRequest().getAttribute("currentUsername");
//            if(currentUsername!=null){
////                将用户信息存入WebSocket会话属性
////                attributes.put("currentUsername",currentUsername);
//                User user = userService.getcurrentuser(currentUsername);
//                if(user!=null){
//                    attributes.put("user",user);
//                    return true;//允许握手
//                }else{
//                    return false;
//                }
//            }else {
////                用户未认证
//                return false;//拒绝WebSocket链接
//            }
//
//        }
//        return false;
//    }
//
//    @Override
//    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
//
//    }
//}
